package com.auxgroup.smarthome.enterpriseapi.apiurl;

import com.auxgroup.smarthome.utils.HttpClientUtil;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by kevin chen on 2017/3/5 0005.
 * 机智云平台api
 */
public enum GEApi {

    /**产品管理 - POST /v1/products/{product_key}/access_token  - 获取token*/
    GET_PRODUCT_TOKEN("/v1/products/%s/access_token", HttpClientUtil.MethodType.METHOD_POST),

    /**内部管理 - GET /v1/enterprises/{enterprise_id}/associations - 获取企业关联的产品列表*/
    ASSOCIATIONS_PRODUCT_LIST("/v1/enterprises/%s/associations?limit=%s&skip=%s",  HttpClientUtil.MethodType.METHOD_GET),

    /**产品管理 - GET /v1/products/{product_key} 获取产品信息*/
    GET_PRODUCT_INFO("/v1/products/%s",  HttpClientUtil.MethodType.METHOD_GET),

    /** 获取产品数据点 GET /v1/products/{product_key}/datapoint*/
    GET_PRODUCT_DATA_POINT("/v1/products/%s/datapoint",   HttpClientUtil.MethodType.METHOD_GET),


    /**设备管理*/
    /**获取设备信息 GET /v1/products/{product_key}/devices */
    GET_DEVICE_MESSAGE("/v1/products/%s/devices?mac=%s",   HttpClientUtil.MethodType.METHOD_GET),

    /**获取设备状态 GET /v1/products/{product_key}/device_detail */
    GET_DEVICE_DETAIL("/v1/products/%s/device_detail?mac=%s",    HttpClientUtil.MethodType.METHOD_GET),

    /** 远程控制设备 POST /v1/products/{product_key}/devices/{did}/control */
    CONTROL_REMOTE_DEVICE("/v1/products/%s/devices/%s/control",   HttpClientUtil.MethodType.METHOD_POST),

    /** 获取单台设备的上下线情况 GET /v1/products/{product_key}/devices/{did}/online */
    GET_DEVICE_ONOFFLINE_DETAIL("/v1/products/%s/devices/%s/online%s", HttpClientUtil.MethodType.METHOD_GET),


    /** 获取设备历史数据 GET /v1/products/{product_key}/devices/{did}/data */
    GET_DEVICE_HISTORY_DATA("/v1/products/%s/devices/%s/data%s", HttpClientUtil.MethodType.METHOD_GET),


    /** 获取设备地理位置分布（实时） GET /v1/products/{product_key}/devices/locations*/
    GET_DEVICE_LOCATION("/v1/products/%s/devices/locations%s",    HttpClientUtil.MethodType.METHOD_GET),

    /** 获取蓝牙设备地理位置分布（实时） GET /v1/products/{product_key}/devices/locations_bt*/
    GET_DEVICE_BT_LOCATION("/v1/products/%s/devices/locations_bt%s",    HttpClientUtil.MethodType.METHOD_GET),

    /** 搜索设备（实时）GET /v1/products/{product_key}/devices/search*/
    SEARCH_DEVICE("/v1/products/%s/devices/search%s", HttpClientUtil.MethodType.METHOD_GET),

    /** 搜索蓝牙设备（实时）GET /v1/products/{product_key}/devices/search_bt*/
    SEARCH_DEVICE_BT("/v1/products/%s/devices/search_bt%s", HttpClientUtil.MethodType.METHOD_GET),

    /**绑定设备*/
    BINDING_DEVICE("/v1/products/%s/devices/bindings", HttpClientUtil.MethodType.METHOD_POST),
    /**解绑设备*/
    UNBINDING_DEVICE("/v1/products/%s/devices/bindings", HttpClientUtil.MethodType.METHOD_DELETE),
    /**获取中控设备的子设备列表*/
    GET_CONTROL_SLAVE("/v1/products/%s/devices/%s/slave?limit=%s&skip=%s", HttpClientUtil.MethodType.METHOD_GET),


    /** 设备报表 */

    /** 获取实时设备激活报表 GET /v1/products/{product_key}/devices/report/rt*/
    GET_REPORT_RT("/v1/products/%s/devices/report/rt?gid=%s", HttpClientUtil.MethodType.METHOD_GET),

    /** 获取设备激活报表 GET /v1/products/{product_key}/devices/report/activation*/
    GET_REPORT_ACTIVATION("/v1/products/%s/devices/report/activation?gid=%s", HttpClientUtil.MethodType.METHOD_GET),

    /***激活历史报表（大数据）GET /v1/products/{product_key}/devices/report/activation/history*/
    GET_REPORT_ACTIVATION_HISTORY("/v1/products/%s/devices/report/activation/history%s", HttpClientUtil.MethodType.METHOD_GET),


    /** 获取活跃设备报表 GET /v1/products/{product_key}/devices/report/liveness*/
    GET_REPORT_LIVENESS("/v1/products/%s/devices/report/liveness?gid=%s", HttpClientUtil.MethodType.METHOD_GET),

    /***活跃设备历史报表（大数据）GET /v1/products/{product_key}/devices/report/liveness/history*/
    GET_REPORT_LIVENESS_HISTORY("/v1/products/%s/devices/report/liveness/history%s" , HttpClientUtil.MethodType.METHOD_GET),

    /** 获取故障设备台数报表 GET /v1/products/{product_key}/fault/report/devices*/
    GET_REPORT_FAULT_DEVICE("/v1/products/%s/fault/report/devices?gid=%s", HttpClientUtil.MethodType.METHOD_GET),

    /** 设备新增详情 GET /v1/products/{product_key}/incr_devices */
    GET_REPORT_DEVICE_NEW_ADD_DETAIL("/v1/products/%s/incr_devices?start=%s&end=%s&country=%s&region=%s&city=%s", HttpClientUtil.MethodType.METHOD_GET),

    /** 设备活跃详情 GET /v1/products/{product_key}/active_devices */
    GET_REPORT_DEVICE_ACTIVE_DETAIL("/v1/products/%s/active_devices?start=%s&end=%s&country=%s&region=%s&city=%s", HttpClientUtil.MethodType.METHOD_GET),

    /** 设备活跃详情 GET /v1/products/{product_key}/active_cycle */
    GET_REPORT_AVICE_CYCLE("/v1/products/%s/active_cycle", HttpClientUtil.MethodType.METHOD_GET),

    /**WiFi 聚合数据 */
    GET_WIFI_AGG_DATA("/v1/products/%s/devices/%s/agg_data%s", HttpClientUtil.MethodType.METHOD_GET),


    /** 设备概览  */
    GET_REPORT_DEVICE_SUMMARY("/v1/products/%s/summary", HttpClientUtil.MethodType.METHOD_GET),


    /**连接时长查询 /v1/products/{product_key}/connection_duration*/
    CONNECTION_DURATION("/v1/products/%s/connection_duration", HttpClientUtil.MethodType.METHOD_GET),
    /** 获取报警设备台数报表 */
    GET_REPORT_ALERT_DEVICE("/v1/products/%s/alert/report/devices?gid=%s",   HttpClientUtil.MethodType.METHOD_GET),

    /** 获取自定义事件历史报表 */
    GET_REPORT_CUSTOM_HISTORY("/v1/products/%s/bt/custom_event?gid=%s&cycle=%s&start=%s&end=%s",   HttpClientUtil.MethodType.METHOD_GET),

    /** 获取故障次数报表 */
    GET_REPORT_FAULT_FREQUENCY("/v1/products/%s/fault/report/events?gid=%s",   HttpClientUtil.MethodType.METHOD_GET),

    /** 获取报警次数报表 @TODO 平台未实现*/
    GET_REPORT_ALERT_FREQUENCY("/v1/products/%s/fault/alert/events?gid=%s",   HttpClientUtil.MethodType.METHOD_GET),


    /** 用户报表*/
    /** 用户新增报表（大数据）*/
    GET_USER_REPORT_NEW_ADD("/v1/products/%s/users/report/new%s", HttpClientUtil.MethodType.METHOD_GET),

    /** 用户总数报表(实时) */
    GET_USER_REPORT_TOTAL("/v1/products/%s/users/report/total?gid=%s", HttpClientUtil.MethodType.METHOD_GET),

    /** 蓝牙用户活跃报表（大数据） */
    GET_USER_REPORT_ACTIVE_BT("/v1/products/%s/users/report/liveness_bt?gid=%s&cycle=%s&start=%s&end=%s", HttpClientUtil.MethodType.METHOD_GET),

    /** 用户管理*/
    /** 搜索用户（实时）*/
    SEARCH_USER("/v1/products/%s/users/search%s", HttpClientUtil.MethodType.METHOD_GET),

    /**获取产品列表*/
    GET_PRODUCT_LIST("/v1/products", HttpClientUtil.MethodType.METHOD_GET),
    /**关联产品*/
    ASSOCIATION_PKS("/v1/products/%s/association", HttpClientUtil.MethodType.METHOD_POST),
    /**企业关联产品列表*/
    ENTERPRISE_ASSOCIATIONS("/v1/enterprises/%s/associations", HttpClientUtil.MethodType.METHOD_GET),


    //----------------------------------  Aux 统计接口 -------------------------------------//
    //----------产品分析-------//
    /**
     *  获取透传数据点
     */
    EXT_DATAPOINT("/v1/aux/product/%s/ext_datapoint", HttpClientUtil.MethodType.METHOD_GET),
    /**
     * 设备概览
     */
    AUX_DEVICE_SUMMARY("/v1/aux/device_summary?device_model=%s", HttpClientUtil.MethodType.METHOD_GET),
    /**
     * 设备趋势
     */
    AUX_DEVICE_TREND("/v1/aux/device_trend?start=%s&end=%s&device_model=%s", HttpClientUtil.MethodType.METHOD_GET),
    /**
     * 设备功能使用情况
     */
    AUX_DATAPOINT_DETAIL("/v1/aux/datapoint_detail?device_model=%s", HttpClientUtil.MethodType.METHOD_GET),
    /**
     * 设备分布情况
     * 分布类型，device代表设备数量分布，fault代表故障设备分布
     */
    AUX_DEVICE_DISTRIBUTION("/v1/aux/device_distribution?type=%s&device_model=%s", HttpClientUtil.MethodType.METHOD_GET),
    /**
     * 设备故障分析情况
     */
    AUX_DEVICE_ERROR_DETAIL("/v1/aux/device_error_detail?device_model=%s", HttpClientUtil.MethodType.METHOD_GET),

    //----------------------用户分析--------------------------//
    /**
     * 用户概览
     */
    AUX_USER_SUMMARY("/v1/aux/user_summary?device_model=%s", HttpClientUtil.MethodType.METHOD_GET),
    /**
     * 用户趋势
     */
    AUX_USER_TREND("/v1/aux/user_trend?start=%s&end=%s&device_model=%s", HttpClientUtil.MethodType.METHOD_GET),
    /**
     * 用户数量分布情况
     */
    AUX_USER_DISTRIBUTION("/v1/aux/user_distribution?device_model=%s", HttpClientUtil.MethodType.METHOD_GET),
    /**
     * 用户增长历史记录
     */
    AUX_USER_INCREASE_HISTORY("/v1/aux/user_incr_history?start=%s&end=%s", HttpClientUtil.MethodType.METHOD_GET),
    /**
     * 用户地理位置分布
     */
    AUX_USER_LOCATION_DISTRIBUTION("/v1/aux/user_location_distribution", HttpClientUtil.MethodType.METHOD_GET),
    /**
     * 用户总数
     */
    AUX_USER_TOTAL("/v1/aux/user_total", HttpClientUtil.MethodType.METHOD_GET),
    //---------------聚合接口----------------//
    AUX_AGG_DATA("/v1/aux/product/%s/devices/%s/agg_data%s", HttpClientUtil.MethodType.METHOD_GET),

    //----------------设备管理---------------//
    LATEST_DATA_BY_CMD_SRC("/v1/products/%s/devices/%s/latest_data?cmd=%s&src=%s", HttpClientUtil.MethodType.METHOD_GET),

    ;

    private String url;

    private String[][] headers;

    private HttpClientUtil.MethodType methodType;

    private static final String BASERURL = "http://smthomeenterpriseapi.auxgroup.com";

    GEApi(String url, HttpClientUtil.MethodType methodType){
        this.url = BASERURL + url;
        this.methodType = methodType;
    }

    public String getUrl() {
        return url;
    }

    public String getUrl(String ... params){
        if(params == null) {
            return String.format(url);
        }
        // 替换掉参数里面的null值
        return String.format(url,Arrays.stream(params).map(param-> param==null?"":param).toArray());
    }

    public static Map<String,String> getHeaderMap(String ... params){
        if (params.length < 1 ){
            throw new RuntimeException("配置或参数错误！");
        }
        Map<String,String> headerMap = new HashMap<String,String>();
        headerMap.put("Authorization","token "+params[0]);
        return headerMap;
    }


    public HttpClientUtil.MethodType getMethodType() {
        return methodType;
    }

}
